package com.popay.common.utils.filter;

//@Order(1)
//@Component
//@WebFilter(filterName = "ConfFilter", urlPatterns = {"/*"})
public class ConfFilter //implements Filter
 {
 /*   private final static Logger log = LoggerFactory.getLogger(ConfFilter.class);

   // @Value("${excludedPageArray}")
    private String excludedPageArray;
 *//* @Autowired
    private SystemUserMapper systemUserMapper;*//*

  //  @Override
    public void init(FilterConfig filterConfig) throws ServletException {}

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String url = request.getRequestURI();
        if (excludedPass(url)) {
            //filterChain.doFilter(new XssHttpServletRequest((HttpServletRequest) servletRequest), response);
            filterChain.doFilter(request, response);
            return;
        } else {
            if (isLogin(request, response) == true) {
                //filterChain.doFilter(new XssHttpServletRequest((HttpServletRequest) servletRequest), response);
                filterChain.doFilter(request, response);
                return;
            } else {
                AjaxJson ajaxJson = new AjaxJson(401, false, "操作失败", "登录验证失效,请重新登录", null, null);
                OutputStream out = response.getOutputStream();
                out.write(JSON.toJSONString(ajaxJson).getBytes("UTF-8"));
                out.flush();
                return;
            }
        }
    }

    @Override
    public void destroy() {

    }

    *//**
     * 忽略列表
     *
     * @param url
     * @return
     *//*
    private boolean excludedPass(String url) {
        String[] arrayslist = excludedPageArray.split(",");
        for (String passurl : arrayslist) {
            if (passurl.indexOf('*') != -1) {
                if (url.startsWith(passurl.replace("*", ""))) {
                    return true;
                }
            } else {
                if (url.equals(passurl)) {
                    return true;
                }
            }
        }
        return false;
    }
    *//**
     * 登录验证 获取请求头
     *
     * @param request
     * @return
     *//*
    private boolean isLogin(HttpServletRequest request, HttpServletResponse response) {
        try {
            final String token = getJwt(request);
            if (token == null || ObjectUtils.isEmpty(token)) {
                return false;
            }
            if (ObjectUtils.isEmpty(token)) {
                return false;
            }
            JwtTokenUtil jwt = new JwtTokenUtil();
            //验证token格式是否正确
            if (jwt.parseToken(token) == false) {
                return false;
            }
            //验证时间
            if (jwt.isTokenExpired(token) == true) {
                return false;
            }
            // TreadLocal 赋值
            Map<String, Object> map = (Map) jwt.getClaimFromToken(token);
            RequestHolder.add(map);
            //刷新token
            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    *//**
     * 获取token
     *
     * @param req
     * @return
     *//*
    private String getJwt(ServletRequest req) {
        HttpServletRequest request = (HttpServletRequest) req;
        final String authHeader = request.getHeader("Authorization");
        if (authHeader == null || ObjectUtils.isEmpty(authHeader)) {
            return null;
        }
        final String token = authHeader.substring(7);// bearer;
        return token;
    }*/

    /**
     * @return void
     * @Description //刷新token  验证 账号 实时状态
     * @Author 晨曦
     * @Date 2019/12/11 15:56
     * @Param [map, response]
     */
    /*private Boolean refreshToken(Map map, HttpServletResponse response) {
        JwtTokenUtil jwt = new JwtTokenUtil();
        Integer count = 0;//systemUserMapper.countStateById((Integer) map.get("id"));
        if (count == 0) {
            response.setHeader("Authorization", null);
            return false;
        } else {
            String token = jwt.generateToken((Integer) map.get("id"), (String) map.get("sub"), jwt.getRandomKey());
            response.setHeader("Authorization", token);
            return true;
        }
    }*/
}
